"                                _                 _
"              __  __ _   _   __| |  __ _  __   __(_) _ __ ___
"              \ \/ /| | | | / _` | / _` | \ \ / /| || '_ ` _ \
"               >  < | |_| || (_| || (_| | _\ V / | || | | | | |
"              /_/\_\ \__,_| \__,_| \__,_|(_)\_/  |_||_| |_| |_|
"              
"                init.vim for neovim on Linux, edited by xuda
"                     authour: xuda ( xuda8868@163.com )
"                        version: 0.6 ( 2021-10-06 )


" ===========================
" === autoload for first time
" ===========================
if empty(glob('~/.config/nvim/autoload/plug.vim'))
    silent !curl -fLo ~/.config/nvim/autoload/plug.vim --create-dirs
		"\ https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
		\ https://raw.fastgit.org/junegunn/vim-plug/master/plug.vim
	autocmd VimEnter * PlugInstall --sync | source $MYVIMRC
endif


" =============================================================================
"                               general settings 
" =============================================================================

set nu                          " set line number
set rnu
"set nocompatible               " close vi compatible, neovim always
set vb t_vb=                    " close alert
set fenc=utf-8                  " set default code
set fencs=utf-8,usc-bom,enu-jp,gb18030,gbk,gb2312,cp936
set ruler                       " show line and columns number 
set showcmd                     " show cmd line
set nobackup                    " close backup and swap
set noshowmode                  " close showmode with airline installed
set laststatus=2                " for airline
set nowritebackup
set noswapfile
set updatetime=300              " Having longer updatetime leads to noticeable 
                                " delays and poor user experience.
set hidden
set wildmenu
set signcolumn=yes

if has("termguicolors")         " true color
    set termguicolors
endif
set cursorcolumn                " set cursor highlight
set cursorline
set splitright                  " split window
set splitbelow
set lazyredraw                  " scroll faster


" =============================================================================
"                               editor settings
" =============================================================================

set cc=80                       " set 80 characters

filetype on                     " file type detect
filetype indent on
filetype plugin on
syntax on                       " high light syntax

set confirm                     " confirm when close without saving
set backspace=indent,eol,start  " backspace settings
  
" indent settings
set autoindent
set cindent                     " c style indent
  
" tab settings
set tabstop=4                   " tab for 4 space
set softtabstop=4       
set shiftwidth=4
set smarttab                    
set expandtab                   " tat to space

set autochdir                   " auto change to file dir "
set scrolloff=5                 " do not use it on internet remote host
  
" file tab
autocmd Filetype sh 
            \ setl shiftwidth=2 tabstop=2 softtabstop=2 expandtab
autocmd Filetype html,css,javascript,xml
            \ setl shiftwidth=2 tabstop=2 softtabstop=2 expandtab


" =============================================================================
"                             search and replace
" =============================================================================

set showmatch                               " highlight match
set matchtime=5                             " highlight time 5ms
set ignorecase                              " ignore case
set smartcase
set hls                                     " highlight search 
set incsearch
set shortmess+=c


" =============================================================================
"                             basic key setting
" =============================================================================

" leader key use space
let mapleader      = "\<space>"
let maplocalleader = ','
noremap ; :

" compile on F5
map <F5> :call CompileRun()<CR>       
func! CompileRun()
    exec "w"
    if &filetype == 'python'
        exec "!python3 %"
    endif
endfunc

" control buffer
"nnoremap <C-Tab> :bn<CR>
"nnoremap <S-C-Tab> :bp<CR>

" window to s key
" Disable the default s key
noremap s <nop>
"nnoremap <C-k> :split<CR>
"nnoremap <C-l> :vsplit<CR>
noremap sk :set nosplitbelow<CR>:split<CR>:set splitbelow<CR>
noremap sj :set splitbelow<CR>:split<CR>
noremap sh :set nosplitright<CR>:vsplit<CR>:set splitright<CR>
noremap sl :set splitright<CR>:vsplit<CR>
noremap so <C-w>o
noremap sq <C-w>q

" Rotate screens
noremap srh <C-w>b<C-w>K
noremap srv <C-w>b<C-w>H

" move cursor in windowswidnow movement
noremap <leader>h <C-w>h
noremap <leader>j <C-w>j
noremap <leader>k <C-w>k
noremap <leader>l <C-w>l

" Resize splits with arrow keys
noremap <up> :res +5<CR>
noremap <down> :res -5<CR>
noremap <left> :vertical resize-5<CR>
noremap <right> :vertical resize+5<CR>

" tab key bind
" Create a new tab with tu
noremap tu :tabe<CR>
" Move around tabs with tn and tp
noremap tn :tabn<CR>
noremap tp :tabp<CR>
" Move the tabs with tmn and tmp
noremap th :-tabmove<CR>
noremap tl :+tabmove<CR>

" indent key
nnoremap < <<
nnoremap > >>

" MYVIMRC key bind
nnoremap <leader>ev :vsp $MYVIMRC<CR>:nohls<CR>
nnoremap <leader>sv :source $MYVIMRC<CR>:nohls<CR>

" select all
map <C-a> ggvG$

" no highlight search :nohls
noremap <leader>no :nohls<CR>

" edit <++> mark
noremap <LEADER>m <Esc>/<++><CR>:nohlsearch<CR>c4l


" =============================================================================
"                              system settings
" =============================================================================

" neovim python settings
let g:python_host_prog  = '/usr/bin/python2'
let g:python3_host_prog = '/usr/bin/python3'

" cache file names
set shadafile=~/.cache/nvim/main.shada

" cursor settings 
"set guicursor=n-v-c:block,i-ci-ve:ver25,r-cr:hor20,o:hor50
            "\,a:blinkwait700-blinkoff400-blinkon250-Cursor/lCursor
            "\,sm:block-blinkwait175-blinkoff150-blinkon175

" =============================================================================
"                                  plugins
" =============================================================================

" Specify a directory for plugins
call plug#begin('~/.config/nvim/plugged')

" startscreen
Plug 'https://gitee.com/xudayjt/vim-startify'
" airline
Plug 'https://gitee.com/xudayjt/vim-airline'
Plug 'https://gitee.com/xudayjt/vim-airline-themes'
" colorscheme
Plug 'https://gitee.com/xudayjt/vim-colorschemes'
Plug 'https://gitee.com/xudayjt/vim-colors-solarized'

" auto complete
" snippet
Plug 'https://gitee.com/xudayjt/ultisnips'
Plug 'https://gitee.com/xudayjt/vim-snippets'
" coc
Plug 'https://gitee.com/xudayjt/coc.nvim', {'branch': 'release'}
" auto pairs
Plug 'https://gitee.com/xudayjt/auto-pairs'
" tmux support in nvim
Plug 'https://gitee.com/xudayjt/tmux-complete.vim'

" nerdtree
Plug 'https://gitee.com/xudayjt/nerdtree', { 'on':  'NERDTreeToggle' }
Plug 'https://gitee.com/xudayjt/nerdtree-git-plugin'

" editor enhancement
" multi line edit
Plug 'https://gitee.com/xudayjt/vim-visual-multi', {'branch': 'master'}
" nerd-commenter
Plug 'https://gitee.com/xudayjt/nerdcommenter'
" indentLine
Plug 'https://gitee.com/xudayjt/indentLine'
" vim-rainbow
Plug 'https://gitee.com/xudayjt/vim-rainbow'
" text align
Plug 'https://gitee.com/xudayjt/vim-easy-align'
" surround
Plug 'https://gitee.com/xudayjt/vim-surround'

" file search
Plug 'https://gitee.com/xudayjt/ctrlp.vim'

" code formater
Plug 'https://gitee.com/xudayjt/vim-autoformat'

" terminal
" float terminal
Plug 'https://gitee.com/xudayjt/vim-floaterm'
" open inner terminal
Plug 'https://gitee.com/xudayjt/vim-terminal-help'
Plug 'https://gitee.com/xudayjt/asyncrun.vim'
Plug 'https://gitee.com/xudayjt/asynctasks.vim'

" tools
" keybings
Plug 'https://gitee.com/xudayjt/vim-which-key', { 'on': ['WhichKey', 'WhichKey!'] }
" sudo support in nvim
Plug 'https://gitee.com/xudayjt/suda.vim'

" Initialize plugin system
call plug#end()

" ===========
" airline 
" ===========
let g:airline_theme = "base16_gruvbox_dark_medium"
let g:airline_extensions = []
let g:airline#extensions#keymap#enabled = 0
let g:airline#extensions#whitespace#enabled = 0
let g:airline#extensions#tabline#enabled = 0
let g:airline#extensions#tabline#tab_nr_type = 1 " tab number
let g:airline#extensions#tabline#show_tab_nr = 1
let g:airline#extensions#tabline#formatter = 'default'
let g:airline#extensions#tabline#buffer_nr_show = 0
let g:airline#extensions#tabline#fnametruncate = 16
let g:airline#extensions#tabline#fnamecollapse = 2
let g:airline#extensions#tabline#buffer_idx_mode = 1
nmap <leader>1 <Plug>AirlineSelectTab1
nmap <leader>2 <Plug>AirlineSelectTab2
nmap <leader>3 <Plug>AirlineSelectTab3
nmap <leader>4 <Plug>AirlineSelectTab4
nmap <leader>5 <Plug>AirlineSelectTab5
nmap <leader>6 <Plug>AirlineSelectTab6
nmap <leader>7 <Plug>AirlineSelectTab7
nmap <leader>8 <Plug>AirlineSelectTab8
nmap <leader>9 <Plug>AirlineSelectTab9
nmap <leader>n <Plug>AirlineSelectPrevTab
nmap <leader>p <Plug>AirlineSelectNextTab
nmap <leader>q :bp<CR>:bd #<CR>

" change symbol on airline
if !exists('g:airline_symbols')
    let g:airline_symbols = {}
endif
let g:airline_symbols.maxlinenr = ''
let g:airline_symbols.linenr = ' ln:'
let g:airline_symbols.colnr = ' :'

" ===========
" theme about config
" ===========
colorscheme gruvbox
set background=dark


" ===========
" nerdtree
" ===========
let g:NERDTreeDirArrowExpandable = '+'
let g:NERDTreeDirArrowCollapsible = '-'
let NERDTreeIgnore = ['\.pyc$']  " except .pyc files
let NERDTreeMinimalUI = 1
let NERDTreeDirArrows = 1
" Exit Vim if NERDTree is the only window left.
autocmd BufEnter * if tabpagenr('$') == 1 && winnr('$') == 1 && 
            \ exists('b:NERDTree') && b:NERDTree.isTabTree() |
            \ quit | endif
" keybing of nerd
nnoremap ff :NERDTreeToggle<CR>


" ===========
" nerdcommenter 
" ===========
let g:NERDSpaceDelims = 1


" ===========
" indentLine
" ===========
let g:indent_guides_guide_size = 1
let g:indent_guides_start_level = 2
let g:indentLine_setConceal = 0


" ===========
" ultisnips
" ===========
let g:UltiSnipsExpandTrigger="<c-l>"


" ===========
" coc
" ===========
" -- coc-plugins
let g:coc_global_extensions = [
    \ 'coc-highlight',
    \ 'coc-snippets',
    \ 'coc-sh',
    \ 'coc-json',
    \ 'coc-html',
    \ 'coc-css',
    \ 'coc-tsserver',
    \ 'coc-prettier',
    \ 'coc-pyright',
    \ 'coc-yaml'
    \ ]

" -- coc-snippet
function! s:check_back_space() abort
  let col = col('.') - 1
  return !col || getline('.')[col - 1]  =~# '\s'
endfunction

inoremap <silent><expr> <TAB>
      \ pumvisible() ? coc#_select_confirm() :
      \ coc#expandableOrJumpable() ? 
      \ "\<C-r>=coc#rpc#request('doKeymap', ['snippets-expand-jump',''])\<CR>":
      \ <SID>check_back_space() ? "\<TAB>" :
      \ coc#refresh()

let g:coc_snippet_next = '<tab>'

" --- coc-highlight
autocmd CursorHold * silent call CocActionAsync('highlight')

" --- coc-prettier
" use <leader>f to format code
vmap <leader>f  <Plug>(coc-format-selected)
nmap <leader>f  <Plug>(coc-format-selected)


" ===========
" vim-rainbow
" ===========
let g:rainbow_active = 1


" ===========
" vim-easy-align
" ===========
" in visual mode
xmap <leader>a <Plug>(EasyAlign)
" in normal mod
nmap <leader>a <Plug>(EasyAlign)


" ===========
" auto-pair
" ===========
let g:AutoPairsShortcutToggle = '<M-p>'
let g:AutoPairsShortcutJump   = '<M-o>'
let g:AutoPairsMultilineClose = 0


" ===========
" vim-visual-multi
" ===========


" ===========
" float vim
" ===========
map <leader>t :FloatermToggle<CR>


" ===========
" vim-terminal
" ===========
let g:terminal_close = 1

" ===========
" suda
" ===========
let g:suda#prompt = 'Password: '
let g:suda_smart_edit = 1


" ===========
" vim-watch-key
" ===========
nnoremap <silent> <leader>      :<c-u>WhichKey '<Space>'<CR>
nnoremap <silent> <localleader> :<c-u>WhichKey  ','<CR>
